#include "ring_queue.hpp"
#include "calculation.h"
#include <unistd.h>
void *Production(void *argv)
{
  while (true)
  {

    // ring_queue<std::pair<int, int>> *r = (ring_queue<std::pair<int, int>> *)argv;
    // auto data = std::make_pair(rand() % 100, rand() % 100);
    // r->push(data);
    // printf("%d ? %d = ?\n", data.first, data.second);
    // sleep(1);
    ring_queue<calculator> *r = (ring_queue<calculator> *)argv;
    std::string symbol = "+-*/";
    calculator c(rand() % 100, symbol[rand() % symbol.size()], rand() % 100);
    r->push(c);
    std::cout << c.show() << std::endl;
    usleep(10000);
  }
}
void *Consumption(void *argv)
{
  while (true)
  {
    // ring_queue<std::pair<int, int>> *r = (ring_queue<std::pair<int, int>> *)argv;
    //  auto data = r->pop();
    //  std::string symbol = "+-*/";
    //  calculator compute(data.first, symbol[rand() % symbol.size()], data.second);
    //  compute.run();
    //  sleep(1);
    ring_queue<calculator> *r = (ring_queue<calculator> *)argv;
    calculator c;
    r->pop(c);
    c();
    usleep(10000);
  }
}
int main()
{
  srand((unsigned int)time(0));
  ring_queue<calculator> *r = new ring_queue<calculator>();
  // ring_queue<std::pair<int, int>> *r = new ring_queue<std::pair<int, int>>();
  pthread_t consumer;
  pthread_t consumer1;
  pthread_t consumer2;
  pthread_t consumer3;
  pthread_t consumer4;
  pthread_t maker;
  pthread_t maker1;
  pthread_t maker2;
  pthread_t maker3;
  pthread_t maker4;
  pthread_create(&maker, 0, Production, (void *)r);
  pthread_create(&maker1, 0, Production, (void *)r);
  pthread_create(&maker2, 0, Production, (void *)r);
  pthread_create(&maker3, 0, Production, (void *)r);
  pthread_create(&maker4, 0, Production, (void *)r);
  pthread_create(&consumer, 0, Consumption, (void *)r);
  pthread_create(&consumer1, 0, Consumption, (void *)r);
  pthread_create(&consumer2, 0, Consumption, (void *)r);
  pthread_create(&consumer3, 0, Consumption, (void *)r);
  pthread_create(&consumer4, 0, Consumption, (void *)r);

  pthread_join(consumer, nullptr);
  pthread_join(maker, nullptr);
  delete r;
  return 0;
}
